package com.xm.xmapp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CircleTextView;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.customview.XLHRatingBar;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_order_comment_infoRequest;
import com.xm.xmapp.request.bean.Get_order_info_by_idRequest;
import com.xm.xmapp.response.bean.ResponseOrderCommentInfo;
import com.xm.xmapp.response.bean.ResponseOrderInfoById;
import com.xm.xmapp.response.bean.OrderBeanResponse;
import com.xm.xmapp.tools.StringTools;

@SuppressLint("SimpleDateFormat")
public class LongRentOrderDetailsActivity extends BaseActivity implements OnClickListener {

	private CustomLayout cl_order_number, cl_order_paytype, cl_return_order_time, cl_max_order_time;
	private TextView title, status, miaoshu, mycomment, name, tv_frozen_money, tv_car_frozen_money, tv_addorder_time,
			tv_order_total_money, go_detail;
	private View view8;
	private OrderBeanResponse orderbean;
	private LinearLayout lin1, pinjia, lin;
	private ImageView back;
	private CircleTextView circleTextView;
	private ResponseOrderCommentInfo bean;// 评价bean
	private XLHRatingBar order_star;

	@Override
	public Object dataChang(int flag, Object data) {
		switch (flag) {
		case Type.A_LongRentOrderDetails.PAY_SUCCESS:
			getData();
			break;
		case Type.A_LongRentOrderDetails.ADDCOMMENT_CODE:
			orderbean.is_comment = "1";
			orderbean.order_status = "3";
			setStatus();
			break;
		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	private void getData() {

		promptDialog.show();
		Get_order_info_by_idRequest requestBean = new Get_order_info_by_idRequest();
		requestBean.order_id = orderbean.order_id;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				ResponseOrderInfoById base = JSON.parseObject(response, ResponseOrderInfoById.class);
				orderbean = base.info;
				setData();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_longrentorderdetails);
	}

	@Override
	public void initView() {

		cl_order_number = (CustomLayout) findViewById(R.id.cl_order_number);
		cl_order_paytype = (CustomLayout) findViewById(R.id.cl_order_paytype);
		cl_return_order_time = (CustomLayout) findViewById(R.id.cl_return_order_time);
		cl_max_order_time = (CustomLayout) findViewById(R.id.cl_max_order_time);

		tv_frozen_money = (TextView) findViewById(R.id.tv_frozen_money);
		tv_car_frozen_money = (TextView) findViewById(R.id.tv_car_frozen_money);
		tv_addorder_time = (TextView) findViewById(R.id.tv_addorder_time);
		tv_order_total_money = (TextView) findViewById(R.id.tv_order_total_money);
		go_detail = (TextView) findViewById(R.id.go_detail);
		title = (TextView) findViewById(R.id.title);
		status = (TextView) findViewById(R.id.status);
		name = (TextView) findViewById(R.id.name);

		miaoshu = (TextView) findViewById(R.id.miaoshu);
		mycomment = (TextView) findViewById(R.id.mycomment);
		lin1 = (LinearLayout) findViewById(R.id.lin1);
		pinjia = (LinearLayout) findViewById(R.id.pinjia);
		lin = (LinearLayout) findViewById(R.id.lin);
		back = (ImageView) findViewById(R.id.back);
		view8 = (View) findViewById(R.id.view8);
		order_star = (XLHRatingBar) findViewById(R.id.order_star);

	}

	@Override
	public void initData() {
		title.setText("订单详情");
		orderbean = (OrderBeanResponse) getIntent().getSerializableExtra("orderbean");
		setData();
	}

	private void setData() {

		if (orderbean != null) {
			name.setText(orderbean.car_number);
			circleTextView = new CircleTextView(this, orderbean.pay_money);
			lin1.removeAllViews();
			lin1.addView(circleTextView);

			tv_addorder_time.setText(StringTools.getTime(orderbean.add_time * 1000, "yyyy-MM-dd"));
			cl_order_number.setText2(orderbean.order_sn);
			cl_order_paytype.setText2(StringTools.getPayWay(orderbean.pay_way_type));
			if (orderbean.return_time != 0)
				cl_return_order_time.setText2(StringTools.getTime(orderbean.return_time * 1000, "yyyy-MM-dd"));
			else {
				view8.setVisibility(View.GONE);
				cl_return_order_time.setVisibility(View.GONE);
			}

			tv_order_total_money.setText(orderbean.order_total_money);

			cl_max_order_time.setText2(StringTools.getLongTime(orderbean.long_rent_repay_date));
			tv_frozen_money.setText(orderbean.frozen_money);
			tv_car_frozen_money.setText(orderbean.car_frozen_money);
			if (orderbean.order_status.equals("2")) {
				pinjia.setVisibility(View.VISIBLE);
				status.setText("续用");
				String nowDate = getIntent().getStringExtra("nowDate");
				int day = StringTools.getLongDay(nowDate, "yyyy-MM-dd",
						StringTools.getLongTime(orderbean.long_rent_repay_date), "yyyy-MM-dd");
				if (day > 0) {
					String days = "" + day;
					String str_text = "距到期剩" + days + "天";
					SpannableStringBuilder style = new SpannableStringBuilder(str_text);
					style.setSpan(new ForegroundColorSpan(Color.RED), 4, 4 + days.length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					tv_addorder_time.setText(style);
				} else if (day == 0) {
					tv_addorder_time.setText("当前合约已到期");
				} else {
					String days = "" + (0 - day);
					String str_text = "已过期" + days + "天";
					SpannableStringBuilder style = new SpannableStringBuilder(str_text);
					style.setSpan(new ForegroundColorSpan(Color.RED), 3, 3 + days.length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					tv_addorder_time.setText(style);
				}
				lin.setVisibility(View.GONE);
			} else {
				if (orderbean.is_comment.equals("1"))
					pinjia.setVisibility(View.GONE);
				else
					pinjia.setVisibility(View.VISIBLE);
				lin.setVisibility(View.GONE);
			}
		}

	}

	@Override
	public void initEvent() {

		back.setOnClickListener(this);
		status.setOnClickListener(this);
		go_detail.setOnClickListener(this);
		ViewTreeObserver vto2 = lin1.getViewTreeObserver();
		vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void onGlobalLayout() {

				lin1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				circleTextView.startCanvas();
			}
		});
		setStatus();
	}

	private void setStatus() {
		if (orderbean.is_comment.equals("1")) {
			get_order_comment_infoRequest();
		}

	}

	private void get_order_comment_infoRequest() {

		Get_order_comment_infoRequest requestBean = new Get_order_comment_infoRequest();
		requestBean.order_id = orderbean.order_id;
		promptDialog.show();

		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				pinjia.setVisibility(View.GONE);
				lin.setVisibility(View.VISIBLE);
				promptDialog.dismiss();
				ResponseOrderCommentInfo base = JSON.parseObject(response, ResponseOrderCommentInfo.class);
				setImg(base);
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
			}
		});

	}

	private void setImg(ResponseOrderCommentInfo base) {
		bean = base;
		order_star.setCountSelected(base.order_star);
		miaoshu.setText(StringTools.getCommentText(base.order_star));
		mycomment.setText(base.content);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.status:

			if (orderbean.order_status.equals("2")) {
				Intent intent = new Intent(MyAppcation.getMyAppcation(), LongReRentOrderActivity.class);
				intent.putExtra("order_id", orderbean.order_id);
				startActivity(intent);

			} else {

				Intent intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderCommentActivity.class);
				intent.putExtra("bean", orderbean);
				startActivity(intent);
			}

			break;
		case R.id.back:
			finish();
			break;
		case R.id.go_detail:
			Intent intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderCommentActivity.class);
			intent.putExtra("bean", orderbean);
			intent.putExtra("commentBean", bean);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

}
